<%@page import="itsm.isperp.framework.core.context.ContextHolder"%><%@ page
	language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%
  String path = request.getContextPath();
			String basePath = ContextHolder.getWebServerName(request);
%>
<style>
select {
    background: url(<%=basePath%>wechat/resources/image/common/select.png) no-repeat center right;
    -webkit-appearance: none;
    background-size: 3.2vw auto;
    line-height: inherit;
    padding-right: 4vw;
    direction: rtl;
    overflow: hidden;
    text-overflow: ellipsis;
    border: none;
    width: 100%;
    font-size: 4vw;
    text-align: right;
    height: 10vw;
}
</style>
<div data-role="content" class="ui-content" role="main">
	<form method="post"
		action="<%=basePath%>api/wechat/setting/newRegister">
		<input type="hidden" name="openid" value="${openid}" />
		<div class="box" style="background-color:#f8f8f8">

			<div class="weui-cell" style="background-color:white">
				<div class="weui-cell__hd">
					<label class="weui-label"><span class="starReq">*</span>手机</label>
				</div>
				<div class="weui-cell__bd weui-cell_primary">
					<input id="mobile" name="mobile" class="weui-input formRequired"
						onchange="searchByMobile()" data-valid="手机" type="text"
						placeholder="请输入手机">
				</div>
				<div class="weui-cell_ft" style="display: none">
					<i class="weui_icon_warn"></i>
				</div>
			</div>


			<div class="weui-cell " style="background-color:white;">
				<div class="weui-cell__hd">
					<label class="weui-label"><span class="starReq">*</span>所属单位</label>
				</div>
				<div class="weui-cell__bd weui-cell_primary">
					<input name="cusDeptId" id="cusDeptId" style="display:none">
					<input id="cusDeptName" name="cusDeptName" class="weui-input"
						type="text" onfocus="cusfocus()" placeholder="请输入您的单位(全称)">
				</div>
				<div class="weui-cell_ft" style="display: none">
					<i class="weui_icon_warn"></i>
				</div>
			</div>
			
			<div class="weui-cell " style="background-color:white;display:none" id="drpDeptDiv">
				<div class="weui-cell__hd">
					<label class="weui-label"><span class="starReq">*</span>所属机构</label>
				</div>
				<div class="weui-cell__bd weui-cell_primary" id="drpDept">
					
				</div>
				<div class="weui-cell_ft" style="display: none">
					<i class="weui_icon_warn"></i>
				</div>
			</div>

			<div class="weui-cell" style="background-color:white">
				<div class="weui-cell__hd">
					<label class="weui-label">座机</label>
				</div>
				<div class="weui-cell__bd weui-cell_primary">
					<input id="tel" name="tel" class="weui-input" type="text"
						placeholder="请输入座机">
				</div>
				<div class="weui-cell_ft" style="display: none">
					<i class="weui_icon_warn"></i>
				</div>
			</div>

			<div class="weui-cell " style="background-color:white">
				<div class="weui-cell__hd">
					<label class="weui-label"><span class="starReq">*</span>申报人</label>
				</div>
				<div class="weui-cell__bd weui-cell_primary">
					<input name="cusUserId" id="cusUserId" style="display:none">
					<input id="name" name="name" class="weui-input formRequired"
						data-valid="名称" type="text" placeholder="请输入名称"
						onfocus="searchUser()">
				</div>
				<div class="weui-cell_ft" style="display: none">
					<i class="weui_icon_warn"></i>
				</div>
			</div>
			<div class="weui-skin_android" id="androidActionsheet"
				style="display: none">
				<div class="weui-mask"></div>
				<div class="weui-actionsheet" style="height:80%;">
					<div class="weui-actionsheet__menu"
						style="overflow:auto;height:100%;"></div>
				</div>
			</div>

			<div class="weui-cells__title">更多</div>
			<div class="weui-cells weui-cells_form">
				<div class="weui-cell" style="background-color:white;">
					<div class="weui-cell__hd">
						<label class="weui-label">邮箱</label>
					</div>
					<div class="weui-cell__bd weui-cell_primary">
						<input id="email" name="" email"" class="weui-input " type="text"
							placeholder="请输入邮箱">
					</div>
					<div class="weui-cell_ft" style="display: none">
						<i class="weui_icon_warn"></i>
					</div>
				</div>
			</div>
			<div class="weui-cell " style="background-color:white">
				<div class="weui-cell__hd">
					<label class="weui-label">联系地址</label>
				</div>
				<div class="weui-cell__bd weui-cell_primary">
					<input id="address" name="address" " class="weui-input "
						type="text" placeholder="请输入联系地址">
				</div>
				<div class="weui-cell_ft" style="display: none">
					<i class="weui_icon_warn"></i>
				</div>
			</div>
			<div>
				<button class="weui-btn weui-btn_primary" type="submit"
					onclick="return takeSubmit();">提交</button>
			</div>
		</div>
	</form>
	<div id="loadingToast" style="opacity: 0; display: none;">
		<div class="weui-mask_transparent"></div>
		<div class="weui-toast">
			<i class="weui-loading weui-icon_toast"></i>
			<p class="weui-toast__content">数据加载中</p>
		</div>
	</div>

	<script type="text/javascript">
		document.title = "注册账号";
		curSearchInput = "";
		$(function() {
			var $searchBar = $('#searchBar'), $searchResult = $('#searchResult'), $searchText = $('#searchText'), $searchInput = $('#searchInput'), $searchClear = $('#searchClear'), $searchOk = $('#searchOk');
			$searchBar.addClass('weui-search-bar_focusing');
			$searchInput.focus();
			function hideSearchResult() {
				$searchResult.hide();
				$searchInput.val('');
			}
			function cancelSearch() {
				hideSearchResult();
				$searchBar.removeClass('weui-search-bar_focusing');
				$searchText.show();
			}

			$searchText.on('click', function() {

			});
			$searchInput.on('blur', function() {
				/* if (!this.value.length)
					cancelSearch(); */
			}).on('input', function() {

				if (curSearchInput == "userSearch") {
					search2(this.value);
				} else if (curSearchInput == "cusSearch") {
					search(this.value);
				}

			});
			$searchClear.on('click', function() {
				hideSearchResult();
				$searchInput.focus();
			});
			$searchOk.on('click', function() {
				$("#search-weui-mask").hide();
				$("body").css("overflow", "auto");
				$("#searchResult").html("");
				if ($("#searchInput").val() != "") {
					if (curSearchInput == "userSearch") {
						$("#name").val($("#searchInput").val());
					} else if (curSearchInput == "cusSearch") {
						//查找是否存在该部门
						$.ajax({
							url : "findDeptCountByName?name=" + $("#searchInput").val(),
							success : function(data) {
								if (data) {
									$("#cusDeptName").val($("#searchInput").val());
									$("#cusDeptName").val(data[0].id);
								}

								else {
									$("#cusDeptName").val("");
									$("#cusDeptId").val("");
								}

							}
						});
					}
				}
				$("#searchInput").val("");
			});
		});

		function searchByMobile() {
			var mobile = $("#mobile").val();
			$.ajax({
				url : "getUserByMobile?mobile=" + mobile,
				success : function(data) {
					if (data.id) {
						$("#cusUserId").val(data.id);
						$("#name").val(data.fullname);
						$("#tel").val(data.tel);
						$("#email").val(data.email);
						$("#address").val(data.address);
						$("#cusDeptName").val(data.department_name);
					}
				}
			});
		}

		function search(v) {
			if (v.length) {
				//实时搜索单位
				var url = "autocompleteUserCustomerList?term=" + v;
				$.ajax({
					url : url,
					success : function(data) {
						$("#searchResult").html("");

						if (data.content.length > 0) {
							for ( var i = 0; i < data.content.length; i++) {
								var andmenu = $("#searchResult");
								andmenu.append("<div class=\"weui-cell weui-cell_access\" onclick=\"choseCustomer('" + data.content[i].id + "','" + data.content[i].name + "')\">" + "<div class=\"weui-cell__bd weui-cell_primary\"><p>" + data.content[i].name + "</p></div>" + "</div>");
							}

						}
					}
				});

				$("#searchResult").show();
			} else {
				$("#searchResult").hide();
			}
		}

		function search2(v) {
			if (v.length) {
				//实时搜索单位
				var url = "autocompleteUserList?term=" + v;
				$.ajax({
					url : url,
					success : function(data) {
						$("#searchResult").html("");

						if (data.content.length > 0) {
							for ( var i = 0; i < data.content.length; i++) {
								var andmenu = $("#searchResult");
								andmenu.append("<div class=\"weui-cell weui-cell_access\" onclick=\"choseUser('" + data.content[i].id + "','" + data.content[i].fullname + "','" + data.content[i].mobile + "','" + data.content[i].tel + "','" + data.content[i].department_name + "')\">" + "<div class=\"weui-cell__bd weui-cell_primary\"><p>" + data.content[i].fullname + "</p></div>" + "</div>");
							}

						}
					}
				});

				$("#searchResult").show();
			} else {
				$("#searchResult").hide();
			}
		}
	</script>
	<script>
		var subing = false;
		function takeSubmit() {
			var validReult = true;
			var message = "请填写：";

			$("form .formRequired").each(function(index, item) {
				var me = $(item);
				var t = me.is("input:checkbox") || me.is("input:radio"), tValue = "";
				if (t) {
					var tName = me.attr("name");
					tValue = me.find("input[name='" + tName + "']:checked").val();
				} else {
					tValue = me.val();
				}

				if ((tValue == "" || tValue == undefined)) {

					message += "" + me.data("valid") + "、";
					validReult = false;
				}
			});

			//salert(message.length);

			message = message.substring(0, message.length - 1);

			if (!validReult) {
				$.weui.alert({
					text : message
				});
				return false;
			}

			var checkResult = false;
			var tname = $("#name").val();
			var tmobile = $("#mobile").val();
			$.ajax({
				url : "checkSave?name=" + tname + "&mobile=" + tmobile,
				async : false,
				success : function(data) {
					if (data.success) {
						checkResult = true;
					} else {
						$.weui.alert({
							text : '该手机号码已注册，请联系服务台4008879987解决'
						});
						checkResult = false;

					}

				}
			});
			if (checkResult == false)
				return false;

			if (subing == false) {
				$(this).hide();
				var $loadingToast = $('#loadingToast');
				if ($loadingToast.css('display') != 'none')
					return;
				$loadingToast.fadeIn(100);
				subing = true;
				$("#formid").submit();
				return true;
			}

		}

		var myDate = new Date();
		$("#dateSelector").DateSelector({
			ctlYearId : 'idYear',
			ctlMonthId : 'idMonth',
			defYear : 2000,
			defMonth : 1,
			defDay : 1,
			ctlDayId : 'idDay',
			minYear : 2000,
			maxYear : 2100
		});

		$("#idMonth").val("");
		$("#idDay").val("");

		var lastInputCus = "";

		function cusfocus() {
			if ($("#cusDeptName").val() == "") {
				curSearchInput = "cusSearch";
				$("#searchInput").val($("#cusDeptName").val());
				$("body").css("overflow", "hidden");
				$("#search-weui-mask").show();
				search($("#cusDeptName").val());
				$("#searchInput").focus();
			}
		}
		function searchUser() {
			if ($("#name").val() == "") {
				curSearchInput = "userSearch";
				$("#searchInput").val($("#name").val());
				$("body").css("overflow", "hidden");
				$("#search-weui-mask").show();
				search2($("#name").val());
				$("#searchInput").focus();
			}
		}
		function choseCustomer(id, name) {
			$("#cusDeptId").val(id);
			$("#cusDeptName").val(name);
			$("#search-weui-mask").hide();
			$("body").css("overflow", "auto");
			$("#searchResult").html("");
			$("#searchInput").val("");
			
			$.ajax({
			  url:"checkCustomerProject?customerId="+id,
			  success:function(data)
			  {
			     if(data.length>1)
			     {
			        var str="";
			        for(var i=0;i<data.length;i++)
			        {
			           var cusName=data[i].cus_department_name;
			           var cusId=data[i].cus_department_id;
			           str+="<option value='"+cusId+"'>"+cusName+"</option>";
			        }
			        str="<select id='userDept' name='userDept'>"+str+"</select>";
			        $("#drpDept").append(str);
			        $("#drpDeptDiv").show();
			     }else
			     {
			        $("#drpDeptDiv").html("");
			        $("#drpDeptDiv").hide();
			     }
			  }
			});
			
		}
		function choseUser(id, name, mobile, tel, departmentName) {
			$("#name").val(name);
			$("#cusUserId").val(id);
			$("#mobile").val(mobile);
			$("#cusDeptName").val(departmentName);
			$("#tel").val(tel);
			$("#search-weui-mask").hide();
			$("body").css("overflow", "auto");
			$("#searchResult").html("");
			$("#searchInput").val("");
		}
	</script>

</div>
